package com.app.twth.ui.search;


import android.view.Gravity;
import android.view.KeyEvent;
import android.view.View;
import android.view.inputmethod.EditorInfo;
import android.widget.TextView;

import androidx.appcompat.app.AlertDialog;
import androidx.recyclerview.widget.GridLayoutManager;
import androidx.recyclerview.widget.LinearLayoutManager;

import com.app.twth.BR;
import com.app.twth.R;
import com.app.twth.base.BaseActivity;
import com.app.twth.bean.FilePathListDTO;
import com.app.twth.databinding.ActivitySearchBinding;
import com.app.twth.databinding.DialogSearchContainerBinding;
import com.app.twth.databinding.DialogSearchIblpnBinding;
import com.app.twth.databinding.DialogSearchTrkBinding;
import com.app.twth.ui.adjust.redamage.ReDamageImgUrlAdapter;
import com.app.twth.utils.EditTextUtil;
import com.app.twth.utils.MyDialogUtil;
import com.app.twth.utils.ToastUtil;
import com.app.twth.utils.Utils;

import java.util.ArrayList;
import java.util.List;

/**
 * @describe :
 */
public class SearchActivity extends BaseActivity<ActivitySearchBinding, SearchViewModel> {
    private String TAG = getClass().getSimpleName();
    int type = 0;

    @Override
    protected void initListener() {
        viewModel.containerLiveData.observe(this, data -> {
            showContainerDialog(data);
        });
        viewModel.iBLpnLiveData.observe(this, data -> {
            showIBLpnDialog(null,data);
        });
        viewModel.iBLpnLiveDatas.observe(this, data -> {
            List<FilePathListDTO> lpnPhotoList = data.getLpnPhotoList();
            List<String>  lpnPhotos = new ArrayList<>();
            if (lpnPhotoList!=null){
                for (FilePathListDTO filePathListDTO:lpnPhotoList){
                    List<String> pathList = filePathListDTO.getPathList();
                    if (pathList!=null){
                        lpnPhotos.addAll(pathList);
                    }
                }
            }
            showIBLpnDialog(lpnPhotos,data.getInventoryDetailList());
        });
        viewModel.oBLpnLiveData.observe(this, data -> {
            showOBLpnDialog(data);
        });
        viewModel.trkLiveData.observe(this, data -> {
            showTrkDialog(data);
        });
        viewModel.errorResultCode.observe(this, code -> {
            showHint((String) code);
        });
        viewModel.isLoading.observe(this, b -> {
            if (b.isShow()) {
                showProgressDialog(b.getMsg());
            } else {
                dismissProgressDialog();
            }
        });
    }

    protected String setTitle() {
        return "Search";
    }

    @Override
    protected int initViewModelId() {
        return BR.viewModel;
    }

    @Override
    protected void initData() {
        setReset("Search");
        binding.tvCode.setOnEditorActionListener(EnterListenter);
    }

    @Override
    protected void onResume() {
        // TODO Auto-generated method stub
        super.onResume();
        bottomLayoutBinding.tvHint.setText("");
        initScan();
    }

    @Override
    protected void onPause() {
        // TODO Auto-generated method stub
        super.onPause();
        unScan();
    }

    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.iv_scan:
                setScan();
                break;
            case R.id.tv_reset:
                barcodeStr = binding.tvCode.getText().toString();
                if (Utils.isEmpty(barcodeStr)) {
                    ToastUtil.showToast(getString(R.string.please_scan_or_input_lower));
                    return;
                }
                getData();
                break;
            case R.id.rb_location:
                if (type == 0) {
                    return;
                }
                type = 0;
                resetView();
                break;
            case R.id.rb_sku:
                if (type == 1) {
                    return;
                }
                type = 1;
                resetView();
                break;
            case R.id.rb_lpn:
                if (type == 2) {
                    return;
                }
                type = 2;
                resetView();
                break;
            case R.id.rb_lable:
                if (type == 3) {
                    return;
                }
                type = 3;
                resetView();
                break;
            case R.id.rb_container:
                if (type == 4) {
                    return;
                }
                type = 4;
                resetView();
                break;
            case R.id.rb_trk:
                if (type == 6) {
                    return;
                }
                type = 6;
                resetView();
                break;
        }
    }

    private void resetView() {
        binding.tvCode.setText("");
        binding.tvCode.requestFocus();

        binding.rbLocation.setChecked(type == 0 ? true : false);
        binding.rbSku.setChecked(type == 1 ? true : false);
        binding.rbLpn.setChecked(type == 2 ? true : false);
        binding.rbLable.setChecked(type == 3 ? true : false);
        binding.rbContainer.setChecked(type == 4 ? true : false);
        binding.rbTrk.setChecked(type == 6 ? true : false);
    }

    private TextView.OnEditorActionListener EnterListenter = new TextView.OnEditorActionListener() {

        @Override
        public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
            if (actionId == EditorInfo.IME_ACTION_SEARCH) {
                barcodeStr = binding.tvCode.getText().toString();
                EditTextUtil.hideInput(mContext);
                if (!Utils.isEmpty(barcodeStr)) {
                    getData();
                }
            }
            return false;
        }
    };

    @Override
    public void getScanninCode() {
        super.getScanninCode();
        binding.tvCode.setText(barcodeStr);
        getData();
    }

    private void getData() {
        showHint("");
        switch (type) {
            case 0://location
                viewModel.searchLocation(barcodeStr);
                break;
            case 1://sku
                viewModel.searchSku(barcodeStr);
                break;
            case 2://IbLpn
                viewModel.searchIbLpn(barcodeStr);
                break;
            case 3://ObLpn
                viewModel.searchObLpn(barcodeStr);
                break;
            case 4://container
                viewModel.searchContainer(barcodeStr);
                break;
            case 6://trk
                viewModel.searchTrk(barcodeStr);
                break;
        }
    }

    private AlertDialog dialog;

    private void showContainerDialog(SearchContainer searchContainer) {
        dialog = MyDialogUtil.getDialog(mContext, initContainerDialogView(searchContainer), Gravity.CENTER);
        dialog.setCanceledOnTouchOutside(true);
        dialog.show();
    }


    private View initContainerDialogView(SearchContainer searchContainer) {
        DialogSearchContainerBinding dialogBinding = DialogSearchContainerBinding.inflate(getLayoutInflater());
        dialogBinding.tvTitle.setText(barcodeStr);
        dialogBinding.tvContent.setText("Drop date: " + searchContainer.getDropDate());
        SearchContainerAdapter adapter = new SearchContainerAdapter(this, searchContainer.getSkuInfoList());

        LinearLayoutManager layoutManager1 = new LinearLayoutManager(this);
        layoutManager1.setOrientation(LinearLayoutManager.VERTICAL);
        dialogBinding.recyclerviewDetail.setLayoutManager(layoutManager1);
        dialogBinding.recyclerviewDetail.setAdapter(adapter);

        dialogBinding.tvCancel.setOnClickListener(v -> {
            if (dialog != null) {
                dialog.dismiss();
            }
        });
        return dialogBinding.getRoot();
    }

    private void showIBLpnDialog(List<String>  lpnPhotos,List<SearchIBLpn> searchIBLpnList) {
        dialog = MyDialogUtil.getDialog(mContext, initIBLpnDialogView(lpnPhotos,searchIBLpnList), Gravity.CENTER);
        dialog.setCanceledOnTouchOutside(true);
        dialog.show();
    }


    private View initIBLpnDialogView(List<String>  lpnPhotos,List<SearchIBLpn> searchIBLpnList) {
        DialogSearchIblpnBinding dialogBinding = DialogSearchIblpnBinding.inflate(getLayoutInflater());
        dialogBinding.tvTitle.setText(barcodeStr);
        switch (type) {
            case 0://location
                dialogBinding.tvHint1.setText("IBLPN");
                break;
            case 1://sku
                dialogBinding.tvHint1.setText("Location");
                dialogBinding.tvHint2.setText("IBLPN(SKUQTY)");
                break;
            case 2://IbLpn
                dialogBinding.tvHint1.setText("Location");
                break;
        }
        if (lpnPhotos==null||lpnPhotos.size()==0){
            dialogBinding.recyclerview.setVisibility(View.GONE);
        }else {
            dialogBinding.recyclerview.setVisibility(View.VISIBLE);
            GridLayoutManager lengthLayoutManager = new GridLayoutManager(mContext, 3);
            dialogBinding.recyclerview.setLayoutManager(lengthLayoutManager);
            dialogBinding.recyclerview.setAdapter(new ReDamageImgUrlAdapter(mContext, lpnPhotos));
        }

        SearchIBLpnAdapter adapter = new SearchIBLpnAdapter(this, searchIBLpnList, type);
        LinearLayoutManager layoutManager1 = new LinearLayoutManager(this);
        layoutManager1.setOrientation(LinearLayoutManager.VERTICAL);
        dialogBinding.recyclerviewDetail.setLayoutManager(layoutManager1);
        dialogBinding.recyclerviewDetail.setAdapter(adapter);

        dialogBinding.tvCancel.setOnClickListener(v -> {
            if (dialog != null) {
                dialog.dismiss();
            }
        });
        return dialogBinding.getRoot();
    }

    private void showOBLpnDialog(List<SearchOBLpn> searchOBLpnList) {
        dialog = MyDialogUtil.getDialog(mContext, initOBLpnDialogView(searchOBLpnList), Gravity.CENTER);
        dialog.setCanceledOnTouchOutside(true);
        dialog.show();
    }


    private View initOBLpnDialogView(List<SearchOBLpn> searchOBLpnList) {
        DialogSearchIblpnBinding dialogBinding = DialogSearchIblpnBinding.inflate(getLayoutInflater());
        dialogBinding.tvTitle.setText(barcodeStr);
        dialogBinding.tvHint1.setText("Picker");
        dialogBinding.tvHint2.setText("SKU(QTY)");

        SearchOBLpnAdapter adapter = new SearchOBLpnAdapter(this, searchOBLpnList, type);
        LinearLayoutManager layoutManager1 = new LinearLayoutManager(this);
        layoutManager1.setOrientation(LinearLayoutManager.VERTICAL);
        dialogBinding.recyclerviewDetail.setLayoutManager(layoutManager1);
        dialogBinding.recyclerviewDetail.setAdapter(adapter);

        dialogBinding.tvCancel.setOnClickListener(v -> {
            if (dialog != null) {
                dialog.dismiss();
            }
        });
        return dialogBinding.getRoot();
    }

    private void showTrkDialog(SearchTrk searchTrk) {
        dialog = MyDialogUtil.getDialog(mContext, initTrkDialogView(searchTrk), Gravity.CENTER);
        dialog.setCanceledOnTouchOutside(true);
        dialog.show();
    }


    private View initTrkDialogView(SearchTrk searchTrk) {
        DialogSearchTrkBinding dialogBinding = DialogSearchTrkBinding.inflate(getLayoutInflater());
        dialogBinding.tvTitle.setText(barcodeStr);
        dialogBinding.tvTime.setText(" Time: " + searchTrk.getTime());
        dialogBinding.tvStatus.setText("Status: " + searchTrk.getStatus());

        dialogBinding.tvCancel.setOnClickListener(v -> {
            if (dialog != null) {
                dialog.dismiss();
            }
        });
        return dialogBinding.getRoot();
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
    }
}
